import type { ColumnSetting } from './types'

// 列设置缓存管理
export class ColumnSettingsManager {
  private static getStorageKey(userId: string, pagePath: string): string {
    return `column-settings-${userId}-${pagePath}`
  }

  // 保存列设置到localStorage
  static saveColumnSettings(settings: ColumnSetting[], userId: string, pagePath: string): void {
    try {
      const key = this.getStorageKey(userId, pagePath)
      localStorage.setItem(key, JSON.stringify(settings))
    } catch (error) {
      console.error('保存列设置失败:', error)
    }
  }

  // 从localStorage加载列设置
  static loadColumnSettings(userId: string, pagePath: string): ColumnSetting[] {
    try {
      const key = this.getStorageKey(userId, pagePath)
      const stored = localStorage.getItem(key)
      if (stored) {
        return JSON.parse(stored)
      }
    } catch (error) {
      console.error('加载列设置失败:', error)
    }
    return []
  }

  // 清除列设置缓存
  static clearColumnSettings(userId: string, pagePath: string): void {
    try {
      const key = this.getStorageKey(userId, pagePath)
      localStorage.removeItem(key)
    } catch (error) {
      console.error('清除列设置失败:', error)
    }
  }
}